package com.mask.roll.edu.controller.front;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mask.roll.common.code.ServiceEnums;
import com.mask.roll.common.constant.RollConstants;
import com.mask.roll.common.controller.BaseController;
import com.mask.roll.common.ordervo.CourseWebVoOrder;
import com.mask.roll.common.util.JwtUtils;
import com.mask.roll.common.util.RollResponse;
import com.mask.roll.edu.client.OrderClient;
import com.mask.roll.edu.entity.Course;
import com.mask.roll.edu.entity.VO.chapter.ChapterVO;
import com.mask.roll.edu.entity.VO.course.CourseFrontVo;
import com.mask.roll.edu.entity.VO.course.CourseWebVo;
import com.mask.roll.edu.service.ChapterService;
import com.mask.roll.edu.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hx
 */
@RestController
@RequestMapping("/edu/front/course")
@Api(tags = "前台课程管理")
public class CourseFrontController extends BaseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    /**
     * 条件查询带分页查询课程
     * @param page
     * @param limit
     * @param courseFrontVo
     * @return
     */
    @PostMapping("/get/{page}/{limit}")
    @ApiOperation("条件获取课程列表")
    public RollResponse getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                           @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Page<Course> pageCourse = new Page<>(page,limit);
        Map<String,Object> map = courseService.getCourseFrontList(pageCourse,courseFrontVo);
        //返回分页所有数据
        return RollResponse.result(ServiceEnums.EDU_FRONT_COURSE_PAGE_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.EDU_FRONT_COURSE_PAGE_SUCCESS.getMessage(),map);
    }

    /**
     * 获取课程详情
     * @param courseId
     * @return
     */
    @GetMapping("/get/info/{courseId}")
    @ApiOperation("获取前台课程详情")
    public RollResponse getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        String memberId = getUserWrapper(request);
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);

        //根据课程id查询章节和小节
        List<ChapterVO> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);

        //查询课程购买状态
        RollResponse response = orderClient.isBuyCourse(courseId, memberId);
        Boolean isBuy = false;
        if (response.getLevel().equals(RollConstants.SUCCESS)) {
            isBuy = true;
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("courseWebVo",courseWebVo);
        map.put("chapterVideoList",chapterVideoList);
        map.put("isBuy",isBuy);
        return RollResponse.result(ServiceEnums.EDU_FRONT_COURSE_DETAIL_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.EDU_FRONT_COURSE_DETAIL_SUCCESS.getMessage(),map);
    }


    /**
     * 根据课程id查询课程信息
     * @param id
     * @return
     */
    @PostMapping("/get/course/info/{id}")
    public CourseWebVoOrder getCourseInfo(@PathVariable("id") String id){
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);

        return courseWebVoOrder;
    }

}












